package com.dkcd.chenshiproject.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.dkcd.chenshiproject.R;
import com.dkcd.chenshiproject.tabHostActivity.MusicPavilionFragment;
import com.dkcd.chenshiproject.tabHostActivity.RecommendFragment;

/**
 *
 * @author ChenShi
 * @version：1.0
 * @date
 * 描述: 首页Fragment
 *
 */
public class IndexMapFragment extends Fragment {

    private String[] tags = {"推荐","音乐馆"};

    public IndexMapFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.e("TAG", "onCreateView: fragment_index_map执行了");
        // Inflate the layout for this fragment
        LinearLayout layout = initTabHost(inflater);
//        return inflater.inflate(R.layout.fragment_index_map, container, false);
        return layout;
    }

    private LinearLayout initTabHost(LayoutInflater inflater){
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.fragment_index_map, null);
        TabHost tabHost = (TabHost)layout.findViewById(R.id.tabhost);
        tabHost.setup();

        LayoutInflater from = LayoutInflater.from(getContext());

        //动态载入xml，不需要Activity
        from.inflate(R.layout.activity_recommend,tabHost.getTabContentView());
        from.inflate(R.layout.activity_music_pavilion,tabHost.getTabContentView());

        //以上创建和添加标签页也可以用如下代码实现
        tabHost.addTab(tabHost.newTabSpec("tab01").setIndicator("推荐").setContent(R.id.tab01));
        tabHost.addTab(tabHost.newTabSpec("tab02").setIndicator("音乐馆").setContent(R.id.tab02));

        //设置标签大小
        for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
            TextView textView = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
            textView.setTextSize(20);
            textView.setGravity(Gravity.CENTER);
            textView.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
            textView.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
        }

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.fragment_tabHost_item, new RecommendFragment());
        ft.commit();

        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                if (tabId.equals("tab01")){
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.replace(R.id.fragment_tabHost_item, new RecommendFragment());
                    ft.commit();
                }else if (tabId.equals("tab02")){
                    Log.e("TAG", "onTabChanged: tab02" );
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.replace(R.id.fragment_tabHost_item, new MusicPavilionFragment());
                    ft.commit();
                }
            }
        });
        return layout;
    }
}